package com.sls.game.supply.deal;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.http.HttpClient;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.PropertiesUtil;
import com.sls.core.util.ServerDateAndTime;
import com.sls.core.util.UtilMD5Encryption;
import com.sls.game.common.DeductionRefund;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.DownstreamUserDAO;
import com.sls.game.dao.EBaiMiDAO;
import com.sls.game.dao.GoodsOrderDAO;
import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.parser.ebaimi.zhifubao.EbaimiTradeParse;
import com.sls.game.pojo.DeductionRefundInfo;
import com.sls.game.pojo.GoodsOrderInfo;
import com.sls.game.pojo.SLSHttpInterfaceInfo;
import com.sls.game.pojo.ebaimi.EbaimiCheckFileInfo;
import com.sls.game.pojo.ebaimi.EbaimiProduct;
import com.sls.game.pojo.ebaimi.EbaimiRequestInfo;
import com.sls.game.pojo.ebaimi.EbaimiResInfo;
import com.sls.game.pojo.ebaimi.ZhifubaoReceiveMsgInfo;

public class EBaiMiTradeDeal {

	private static Logger log = Logger.getLogger(EBaiMiTradeDeal.class);
	private static String filename = "ebaimi.properties";

	/**
	 * 易百米交易结果处理
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param head
	 * @param goodsOrderDAO
	 * @param goodsSupplyManageDAO
	 * @param eBaiMiDAO
	 * @param downstreamUserDAO
	 * @return
	 */
	public static String dealTradeResultOfEBaiMi(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, String orderTime,
			String paymentOrderNo, Head head, GoodsOrderDAO goodsOrderDAO,
			GoodsSupplyManageDAO goodsSupplyManageDAO, EBaiMiDAO eBaiMiDAO,
			DownstreamUserDAO downstreamUserDAO) {
		// 调用交易方法
		EbaimiResInfo resInfo = trade(info, originalGoodsId, orderId,
				orderTime, paymentOrderNo, head, goodsOrderDAO,
				goodsSupplyManageDAO, eBaiMiDAO, downstreamUserDAO);
		// 返回给上层对象
		SLSHttpInterfaceInfo sLSHttpInterfaceInfo = new SLSHttpInterfaceInfo();
		String resultno = "";
		String retCode = "";
		if (resInfo != null) {
			resultno = resInfo.getStat_goodsJorder();
			retCode = resInfo.getRetcode();
		} else {
			resultno = "8";
		}
		if ("000000".equals(retCode)) {
			String supOrderSuccessTime = ServerDateAndTime.getNowDateTime();
			if ("1".equals(resultno) || "2".equals(resultno)) { // 处理中
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_UNDERWAY");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
			} else if ("0".equals(resultno) || "2".equals(resultno)) {
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_SUCCESS");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
				sLSHttpInterfaceInfo
						.setSupOrderSuccessTime(supOrderSuccessTime);
			} else if ("8".equals(resultno)) {
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_DUBIOUS");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
			} else if ("9".equals(resultno)) { // 失败
				refundOfOrderFailed(info, orderId, orderTime, paymentOrderNo,
						head, goodsOrderDAO, downstreamUserDAO,
						sLSHttpInterfaceInfo);
			} else {
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_DUBIOUS");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
			}
		} else {
			refundOfOrderFailed(info, orderId, orderTime, paymentOrderNo, head,
					goodsOrderDAO, downstreamUserDAO, sLSHttpInterfaceInfo);
		}
		sLSHttpInterfaceInfo.setGameAccount(info.getGameAccount());
		sLSHttpInterfaceInfo.setUnitPrice(info.getUnitPrice());
		sLSHttpInterfaceInfo.setBuyNum(info.getBuyNum());

		String totalPrice = info.getTotalPrice();
		sLSHttpInterfaceInfo.setTotalPrice(totalPrice);
		Gson gson = CommonUtil.gson();
		String resultBody = gson.toJson(sLSHttpInterfaceInfo);

		return resultBody;
	}

	/**
	 * 提卡交易处理
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param head
	 * @param goodsOrderDAO
	 * @param goodsSupplyManageDAO
	 * @param eBaiMiDAO
	 * @param downstreamUserDAO
	 * @return
	 */
	public static String dealGetCardResultOfEBaiMi(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, String orderTime,
			String paymentOrderNo, Head head, GoodsOrderDAO goodsOrderDAO,
			GoodsSupplyManageDAO goodsSupplyManageDAO, EBaiMiDAO eBaiMiDAO,
			DownstreamUserDAO downstreamUserDAO) {
		// 调用交易方法
		EbaimiResInfo resInfo = trade(info, originalGoodsId, orderId,
				orderTime, paymentOrderNo, head, goodsOrderDAO,
				goodsSupplyManageDAO, eBaiMiDAO, downstreamUserDAO);
		// 返回给上层对象
		SLSHttpInterfaceInfo sLSHttpInterfaceInfo = new SLSHttpInterfaceInfo();

		StringBuilder strBui = new StringBuilder();
		String cardname = null;
		String cardnum = null;
		String cardIdPwd = null;
		// 上游订单号
		String oid_goodsorder = null;

		if (resInfo != null && "000000".equals(resInfo.getRetcode())) {
			String card_goods = resInfo.getCard_goods();
			if (StringUtils.isNotEmpty(card_goods)) {
				String[] cards = card_goods.split("\\|");
				if (cards != null) {
					for (String card : cards) {
						String[] cardPwd = card.split(",");
						if (cardPwd != null) {
							String cardno = cardPwd[0];
							if (StringUtils.isNotEmpty(cardno)) {
								strBui.append(cardno); // 卡号
								strBui.append(",");
							}
							String cardpws = cardPwd[1];
							if (StringUtils.isNotEmpty(cardpws)) {
								strBui.append(cardpws); // 卡密
							}
						}
						strBui.append(";");
						cardname = resInfo.getName_goods();
						cardnum = resInfo.getAmt_balcur();
						oid_goodsorder = resInfo.getOid_goodsorder();
					}
				}
				String str = strBui.toString();
				if (StringUtils.isNotEmpty(str)) {
					cardIdPwd = str.substring(0, str.length() - 1);
				}
				GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
				goodsOrderInfo.setCardNoPwd(cardIdPwd);
				goodsOrderInfo.setSupOrderId(oid_goodsorder);

				if (cardIdPwd != null && !("".equals(cardIdPwd))) {
					goodsOrderInfo.setStatus("1"); // 成功

					String time = ServerDateAndTime.getNowDateTime();
					goodsOrderInfo.setSupOrderSuccessTime(time);
				} else {
					goodsOrderInfo.setStatus("0"); // 失败
				}
				try {
					// 提卡返回更新订单
					updateGoodsOrder(orderId, head, goodsOrderInfo,
							goodsOrderDAO);
				} catch (Exception e) {
					log.error("请求ID：" + head.getRequestId()
							+ "根据易百米提卡接口返回的结果更新订单时出现异常！", e);
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setCardname(cardname);
				sLSHttpInterfaceInfo.setCardnum(cardnum);
				sLSHttpInterfaceInfo.setCardNoPwdExptime(cardIdPwd);
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);

				Gson gson = CommonUtil.gson();
				String resultBody = gson.toJson(sLSHttpInterfaceInfo);

				return resultBody;
			}
		}
		return null;
	}

	private static void refundOfOrderFailed(SLSHttpInterfaceInfo info,
			String orderId, String orderTime, String paymentOrderNo, Head head,
			GoodsOrderDAO goodsOrderDAO, DownstreamUserDAO downstreamUserDAO,
			SLSHttpInterfaceInfo sLSHttpInterfaceInfo) {
		GoodsOrderInfo goodsOrderInfo1 = new GoodsOrderInfo();
		DeductionRefundInfo dedInfo = null;
		String paymentStatus = "";
		// 退款
		dedInfo = DeductionRefund.invokeDeductRefund(info, orderId, "1",
				orderTime, paymentOrderNo, head, goodsOrderDAO);
		if (dedInfo != null) {
			paymentStatus = dedInfo.getPaymentStatus();
		}
		if ("1".equals(paymentStatus)) { // 成功
			goodsOrderInfo1.setIsPay("3"); // 退款成功
			// 退款成功，通知下游用户
			DeductionRefund.noticeUserRefundMsg(orderId, dedInfo,
					goodsOrderDAO, downstreamUserDAO, null);
		} else if ("2".equals(paymentStatus)) {
			goodsOrderInfo1.setIsPay("2"); // 退款失败
		}
		try {
			// 更新订单
			updateGoodsOrder(orderId, head, goodsOrderInfo1, goodsOrderDAO);
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "根据退款状态更新订单异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	/**
	 * 交易请求
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param head
	 * @param goodsOrderDAO
	 * @param goodsSupplyManageDAO
	 * @param eBaiMiDAO
	 * @param downstreamUserDAO
	 * @return
	 */
	private static EbaimiResInfo trade(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, String orderTime,
			String paymentOrderNo, Head head, GoodsOrderDAO goodsOrderDAO,
			GoodsSupplyManageDAO goodsSupplyManageDAO, EBaiMiDAO eBaiMiDAO,
			DownstreamUserDAO downstreamUserDAO) {
		String parameter = assembleParameterOfTrade(info, originalGoodsId,
				orderId, orderTime, eBaiMiDAO);
		EbaimiResInfo resInfo = sendHttpRequestOfDirectTrade(parameter, head);
		// 上游订单结果
		String orderResult = "";
		String supOrderId = "";
		String supOrderTime = "";
		String retCode = "";
		if (resInfo != null) {
			// 订单结果
			orderResult = resInfo.getStat_goodsJorder();
			supOrderId = resInfo.getOid_goodsorder();
			supOrderTime = resInfo.getDt_create();
			retCode = resInfo.getRetcode();
		}
		if ("000000".equals(retCode)) {
			String status = "2";
			if ("1".equals(orderResult) || "2".equals(orderResult)) { // 处理中
				status = "2";
			} else if ("0".equals(orderResult)) { // 充值成功
				status = "1";
			} else if ("9".equals(orderResult)) { // 充值失败
				status = "0";
			} else {
				status = "3"; // 存疑
			}

			GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
			goodsOrderInfo.setStatus(status);
			goodsOrderInfo.setResult(orderResult);
			goodsOrderInfo.setSupOrderId(supOrderId);
			goodsOrderInfo.setSupOrderSuccessTime(supOrderTime);
			try {
				// 根据交易返回的结果更新订单表
				updateGoodsOrder(orderId, head, goodsOrderInfo, goodsOrderDAO);
			} catch (Exception e) {
				log.error("请求ID：" + head.getRequestId() + "根据易百米返回的结果更新订单异常！",
						e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		} else { // 失败
			String status = "0";
			GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
			goodsOrderInfo.setStatus(status);
			goodsOrderInfo.setResult(orderResult);
			goodsOrderInfo.setSupOrderId(supOrderId);
			goodsOrderInfo.setSupOrderSuccessTime(supOrderTime);
			try {
				// 根据交易返回的结果更新订单表
				updateGoodsOrder(orderId, head, goodsOrderInfo, goodsOrderDAO);
			} catch (Exception e) {
				log.error("请求ID：" + head.getRequestId() + "根据易百米返回的结果更新订单异常！",
						e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}
		return resInfo;
	}

	/**
	 * 更新订单
	 * 
	 * @param orderId
	 * @param head
	 * @param info
	 * @param goodsOrderDAO
	 */
	private static void updateGoodsOrder(String orderId, Head head,
			GoodsOrderInfo goodsOrderInfo, GoodsOrderDAO goodsOrderDAO) {
		goodsOrderInfo.setOrderId(orderId);
		goodsOrderInfo.setSynOrAsy("0"); // 同步通知标识
		// 数据更新
		try {
			goodsOrderDAO.update(goodsOrderInfo);
		} catch (Exception e) {
			log.error(
					"updateGoodsOrder处理[" + head.getRequestId() + "]的请求出现异常！",
					e);
		}
	}

	private static EbaimiResInfo sendHttpRequestOfDirectTrade(String parameter,
			Head head) {
		EbaimiResInfo resInfo = null;
		HttpClient httpClient = new HttpClient();
		String tradeAddress = PropertiesUtil.getValue(
				"ebaimi.interface.recharge.address", filename);
		try {
			// 对返回的gson格式字符串数据进行解析
			Map<String, String> map = new HashMap<String, String>();
			map.put("parameter", parameter);
			log.info("请求ID：" + head.getRequestId() + "--发送到易百米订单交易请求参数："
					+ parameter);
			String gsonStr = httpClient.doPost(tradeAddress, map);
			log.info("请求ID：" + head.getRequestId() + "--易百米订单交易返回给手拉手的请求结果："
					+ gsonStr);
			if (StringUtils.isNotEmpty(gsonStr)) {
				resInfo = CommonUtil.gson().fromJson(gsonStr,
						EbaimiResInfo.class);
			}
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米订单交易请求异常！", e);
		}
		return resInfo;
	}

	private static String assembleParameterOfTrade(SLSHttpInterfaceInfo info,
			String originalGoodsId, String orderId, String orderTime,
			EBaiMiDAO eBaiMiDAO) {
		String parameter = "";
		String loginId = PropertiesUtil.getValue(
				"ebaimi.interface.oid.loginid", filename);
		EbaimiProduct ep = eBaiMiDAO.queryByGoodsId(originalGoodsId);
		if (ep != null) {
			String oid_biz = ep.getOid_biz();
			String buyNum = info.getBuyNum();
			orderTime = orderTime.replace("-", "");
			String price = ep.getPrice();
			String data = loginId + oid_biz + originalGoodsId + buyNum
					+ orderId + orderTime + price;
			String sign = UtilMD5Encryption.createMD5Encryption(data)
					.toUpperCase();
			EbaimiRequestInfo reqInfo = new EbaimiRequestInfo();
			reqInfo.setOid_loginid(loginId);
			reqInfo.setOid_biz(oid_biz);
			reqInfo.setOid_goodsitem(originalGoodsId);
			reqInfo.setAmt_ware(buyNum);
			reqInfo.setJno_cli(orderId);
			reqInfo.setDt_cli(orderTime);
			reqInfo.setPrice(price);
			reqInfo.setSign(sign);
			parameter = CommonUtil.gson().toJson(reqInfo);
		}
		return parameter;
	}

	/**
	 * 支付宝充值处理
	 * 
	 * @param info
	 * @param originalGoodsId
	 * @param orderId
	 * @param orderTime
	 * @param paymentOrderNo
	 * @param head
	 * @param goodsOrderDAO
	 * @param goodsSupplyManageDAO
	 * @param eBaiMiDAO
	 * @param downstreamUserDAO
	 * @return
	 * @author Wangxuegang
	 */
	public static String dealTradeOfZhifubao(SLSHttpInterfaceInfo info,
			String orderId, String orderTime, String paymentOrderNo, Head head,
			GoodsOrderDAO goodsOrderDAO,
			GoodsSupplyManageDAO goodsSupplyManageDAO,
			DownstreamUserDAO downstreamUserDAO) {
		// 合作方的商户ID
		String oid_reguser = PropertiesUtil.getValue(
				"ebaimi.zhifubao.oid_reguser", "ebaimi.properties");
		GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
		goodsOrderInfo.setRegisterAccount(oid_reguser);
		goodsOrderInfo.setOrderId(orderId);
		// 数据更新
		try {
			goodsOrderDAO.update(goodsOrderInfo);
		} catch (Exception e) {
			log.error("处理[" + head.getRequestId() + "]的请求出现异常！", e);
		}

		// 调用交易方法
		ZhifubaoReceiveMsgInfo resInfo = tradeOfZhifubao(info, orderId, head,
				goodsOrderDAO);
		// 返回给上层对象
		SLSHttpInterfaceInfo sLSHttpInterfaceInfo = new SLSHttpInterfaceInfo();
		if (resInfo != null) {
			String retcode = resInfo.getRetcode();
			String stat_goodsbill = resInfo.getSta_goodsbill();
			if ("000000".equals(retcode)) { // ===========================响应成功
				if ("0".equals(stat_goodsbill)) { // 成功
					String supOrderSuccessTime = ServerDateAndTime
							.getNowDateTime();
					sLSHttpInterfaceInfo.setResMsg("SUCCESS");
					sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
					sLSHttpInterfaceInfo.setStatus("ORDER_SUCCESS");
					sLSHttpInterfaceInfo.setSupOrderNo(orderId);
					sLSHttpInterfaceInfo
							.setSupOrderSuccessTime(supOrderSuccessTime);
				} else if ("2".equals(stat_goodsbill)) { // 处理中
					sLSHttpInterfaceInfo.setResMsg("SUCCESS");
					sLSHttpInterfaceInfo.setFailedCode("209");
					sLSHttpInterfaceInfo.setFailedReason("处理中");
					sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
					sLSHttpInterfaceInfo.setStatus("ORDER_UNDERWAY");
					sLSHttpInterfaceInfo.setSupOrderNo(orderId);
				} else if ("9".equals(stat_goodsbill)) { // 失败
					sLSHttpInterfaceInfo.setResMsg("FAILED");
					sLSHttpInterfaceInfo.setFailedCode("208");
					sLSHttpInterfaceInfo.setFailedReason("充值失败");
					sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
					sLSHttpInterfaceInfo.setStatus("ORDER_FAILED");
					sLSHttpInterfaceInfo.setSupOrderNo(orderId);
				} else { // [默认]处理中
					sLSHttpInterfaceInfo.setResMsg("SUCCESS");
					sLSHttpInterfaceInfo.setFailedCode("209");
					sLSHttpInterfaceInfo.setFailedReason("处理中");
					sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
					sLSHttpInterfaceInfo.setStatus("ORDER_UNDERWAY");
					sLSHttpInterfaceInfo.setSupOrderNo(orderId);
				}
			} else if ("0311".equals(retcode)) { // ===============供货异常或支付宝供货异常,根据与易百米技术沟通后，此种情况订单状态应为"处理中"
				sLSHttpInterfaceInfo.setResMsg("SUCCESS");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_UNDERWAY");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
			} else { // ============================响应失败，订单作失败处理
				// 更新订单状态
				GoodsOrderInfo orderInfo = new GoodsOrderInfo();
				orderInfo.setStatus("0"); // 失败
				// 更新订单
				updateGoodsOrder(orderId, head, orderInfo, goodsOrderDAO);
				refundOfOrderFailed(info, orderId, orderTime, paymentOrderNo,
						head, goodsOrderDAO, downstreamUserDAO,
						sLSHttpInterfaceInfo);

				sLSHttpInterfaceInfo.setResMsg("FAILED");
				sLSHttpInterfaceInfo.setFailedCode("208");
				sLSHttpInterfaceInfo.setFailedReason("充值失败");
				sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
				sLSHttpInterfaceInfo.setStatus("ORDER_FAILED");
				sLSHttpInterfaceInfo.setSupOrderNo(orderId);
			}
		} else { //存疑
			GoodsOrderInfo ord = new GoodsOrderInfo();
			ord.setStatus("3");
			updateGoodsOrder(orderId, head, ord, goodsOrderDAO);
			
			sLSHttpInterfaceInfo.setResMsg("SUCCESS");
			sLSHttpInterfaceInfo.setFailedCode("210");
			sLSHttpInterfaceInfo.setFailedReason("订单存疑");
			sLSHttpInterfaceInfo.setPtOrderNo(info.getPtOrderNo());
			sLSHttpInterfaceInfo.setStatus("ORDER_DUBIOUS"); // 存疑
			sLSHttpInterfaceInfo.setSupOrderNo(orderId);
		}
		sLSHttpInterfaceInfo.setGameAccount(info.getGameAccount());
		sLSHttpInterfaceInfo.setUnitPrice(info.getUnitPrice());
		sLSHttpInterfaceInfo.setBuyNum(info.getBuyNum());
		String totalPrice = info.getTotalPrice();
		sLSHttpInterfaceInfo.setTotalPrice(totalPrice);
		Gson gson = CommonUtil.gson();
		String resultBody = gson.toJson(sLSHttpInterfaceInfo);

		return resultBody;
	}

	/**
	 * 
	 * @param info
	 * @param orderId
	 * @param head
	 * @param goodsOrderDAO
	 * @return
	 * @author Wangxuegang
	 */
	private static ZhifubaoReceiveMsgInfo tradeOfZhifubao(
			SLSHttpInterfaceInfo info, String orderId, Head head,
			GoodsOrderDAO goodsOrderDAO) {
		String address = PropertiesUtil.getValue(
				"ebaimi.zhifubao.trade.address", "ebaimi.properties");
		String parameter = assembleParaOfZhifubao(info, orderId);
		ZhifubaoReceiveMsgInfo resInfo = sendHttpRequestOfZhifubao(address,
				parameter, head);
		if (resInfo != null) {
			// 易百米订单号
			String oid_goodsorder = resInfo.getOid_goodsorder();
			// 易百米订单状态
			String stat_goodsbill = resInfo.getSta_goodsbill();
			// Ebaimi创建订单的时间
			String dt_create = resInfo.getDt_create();
			// 自己平台订单号
			String jno_cli = resInfo.getJno_cli();

			String status = "2"; // 自己平台订单状态:处理中

			if ("0".equals(stat_goodsbill)) { // 成功
				status = "1";
			} else if ("2".equals(stat_goodsbill)) { // 处理中
				status = "2";
			} else if ("9".equals(stat_goodsbill)) { // 失败
				status = "0";
			} else {
				status = "2";
			}
			GoodsOrderInfo goodsOrderInfo = new GoodsOrderInfo();
			goodsOrderInfo.setStatus(status);
			goodsOrderInfo.setOrderId(jno_cli);
			goodsOrderInfo.setResult(stat_goodsbill);
			goodsOrderInfo.setSupOrderId(oid_goodsorder);
			goodsOrderInfo.setSupOrderSuccessTime(dt_create);
			// 根据交易返回的结果更新订单表
			updateGoodsOrder(orderId, head, goodsOrderInfo, goodsOrderDAO);
		}
		return resInfo;
	}

	/**
	 * 
	 * @param address
	 * @param parameter
	 * @param head
	 * @return
	 * @author Wangxuegang
	 */
	private static ZhifubaoReceiveMsgInfo sendHttpRequestOfZhifubao(
			String address, String parameter, Head head) {
		HttpClient httpClient = new HttpClient();
		String supplyEncode = "UTF-8";
		try {
			// 对返回的gson格式字符串数据进行解析
			log.info("请求ID：" + head.getRequestId() + "--发送到易百米订单交易请求参数："
					+ parameter);
			String xmlStr = httpClient.doPost(address, parameter, supplyEncode);
			log.info("易百米响应消息：" + xmlStr);
			ZhifubaoReceiveMsgInfo parseInfo = EbaimiTradeParse
					.parseTrade(xmlStr);
			return parseInfo;
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米订单交易请求异常！", e);
		}
		return null;
	}

	/**
	 * 
	 * @param info
	 * @param orderId
	 * @return
	 * @throws ServerDisponseException
	 * @author Wangxuegang
	 */
	private static String assembleParaOfZhifubao(SLSHttpInterfaceInfo info,
			String orderId) throws ServerDisponseException {
		// 业务类型
		String oid_biz = PropertiesUtil.getValue("ebaimi.zhifubao.oid_biz",
				"ebaimi.properties");
		// 易百米平台的商品编号
		String oid_goodsitem = PropertiesUtil.getValue(
				"ebaimi.zhifubao.oid_goodsitem", "ebaimi.properties");
		// 合作方的商户ID
		String oid_reguser = PropertiesUtil.getValue(
				"ebaimi.zhifubao.oid_reguser", "ebaimi.properties");
		// 密钥
		String key = PropertiesUtil.getValue("ebaimi.zhifubao.key",
				"ebaimi.properties");
		if (oid_biz != null && !("".equals(oid_biz))) {
			oid_biz = oid_biz.trim();
		}
		if (oid_goodsitem != null && !("".equals(oid_goodsitem))) {
			oid_goodsitem = oid_goodsitem.trim();
		}
		if (oid_reguser != null && !("".equals(oid_reguser))) {
			oid_reguser = oid_reguser.trim();
		}
		if (key != null && !("".equals(key))) {
			key = key.trim();
		}
		StringBuilder strBui = new StringBuilder();
		strBui.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBui.append("<root>");
		strBui.append("<oid_biz>");
		strBui.append(oid_biz);
		strBui.append("</oid_biz>");
		strBui.append("<oid_goodsitem>");
		strBui.append(oid_goodsitem);
		strBui.append("</oid_goodsitem>");
		String buyNum = info.getBuyNum();
		strBui.append("<amt_ware>");
		strBui.append(buyNum);
		strBui.append("</amt_ware>");
		strBui.append("<jno_cli>");
		strBui.append(orderId);
		strBui.append("</jno_cli>");
		strBui.append("<oid_reguser>");
		strBui.append(oid_reguser);
		strBui.append("</oid_reguser>");
		// 游戏玩家账号
		String gameAccount = info.getGameAccount();
		strBui.append("<uid_cli>");
		strBui.append(gameAccount);
		strBui.append("</uid_cli>");
		strBui.append("<price>");
		strBui.append("1");
		strBui.append("</price>");
		String data = key + oid_reguser;
		String sign = UtilMD5Encryption.createMD5Encryption(data);
		if (sign != null && !("".equals(sign))) {
			sign = sign.trim().toUpperCase();
		}
		strBui.append("<sign>");
		strBui.append(sign);
		strBui.append("</sign>");
		strBui.append("</root>");

		String result = strBui.toString().trim();
		return result;
	}

	/**
	 * 
	 * @param sLSHttpInterfaceInfo
	 * @param originalGoodsId
	 * @param eBaiMiDAO
	 * @return
	 */
	public static SLSHttpInterfaceInfo getGoodsDetailOfEbaiMi(
			SLSHttpInterfaceInfo sLSHttpInterfaceInfo, String originalGoodsId,
			EBaiMiDAO eBaiMiDAO) {
		EbaimiProduct pro = eBaiMiDAO.queryByGoodsId(originalGoodsId);
		String remark = pro.getRemark();
		sLSHttpInterfaceInfo.setNoteinfo("提示：" + remark);
		sLSHttpInterfaceInfo.setGameUnit("元");

		return sLSHttpInterfaceInfo;
	}

	/**
	 * 解析流中的账单信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @date 2013-05-16
	 */
	public static List<EbaimiCheckFileInfo> parseBill(String responseStr) {
		String encodeXmlStr = responseStr;
		// 加载资源文件 转化为一个输入流
		byte[] xmlStrByte = encodeXmlStr.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(xmlStrByte);

		String insertTime = ServerDateAndTime.getNowDateTimeOfFormat();
		List<EbaimiCheckFileInfo> list = new ArrayList<EbaimiCheckFileInfo>();
		String encoding = "UTF-8";
		String temp = null;
		try {
			InputStreamReader read = new InputStreamReader(in, encoding);
			BufferedReader bufr = new BufferedReader(read);
			int lineIndex = 0;
			while (true) {
				EbaimiCheckFileInfo ebaimiCheckFileInfo = new EbaimiCheckFileInfo();
				temp = bufr.readLine();
				if (temp == null) {
					break;
				} else {
					if (lineIndex > 1) {
						String[] strArr = temp.split(",");
						ebaimiCheckFileInfo.setOid_goodsorder(strArr[0]);
						ebaimiCheckFileInfo.setDt_create(strArr[1]);
						ebaimiCheckFileInfo.setGoodsName(strArr[2]);
						ebaimiCheckFileInfo.setJno_cli(strArr[3]);
						ebaimiCheckFileInfo.setAgent_dt_create(strArr[4]);
						ebaimiCheckFileInfo.setBuyNum(strArr[5]);
						ebaimiCheckFileInfo.setTradeMoney(strArr[6]);
						ebaimiCheckFileInfo.setStatus(strArr[7]);
						ebaimiCheckFileInfo.setInsertTime(insertTime);
						list.add(ebaimiCheckFileInfo);
					}
					lineIndex++;
				}
			}
			read.close();
		} catch (Exception e) {
			log.error(e);
		}
		return list;
	}

	// public static void main(String[] args) throws
	// UnsupportedEncodingException{
	// StringBuilder sb = new StringBuilder();
	// sb.append("交易笔数 :2,交易总金额:142.5,");
	// sb.append("\n");
	// sb.append("ebaimi订单号,订单创建时间,商品名称,代理商流水号,代理商订单创建时间,数量,交易额,状态");
	// sb.append("\n");
	// sb.append("2011072700188247,2012-04-05 10:01:41,网易一卡通30元(全国),20113212323,null,4,114.00,9");
	// sb.append("\n");
	// sb.append("2011072700188246,2012-04-05 09:31:14,网易一卡通30元(全国),20113212321,null,1,28.50,9");
	//
	// String str = sb.toString().trim();
	// parseBill(str);
	// }

}
